package pipecleaner.agent.core;

import java.io.FileNotFoundException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;

/**
 * 
 */
public class HttpAgent implements Agent
{  
	private static final String CONTEXT_ROOT = "/";
	
    /** Logger. */
    private static Log log = LogFactory.getLog(HttpAgent.class);
	
    private Server server;
	
    private Configuration config;
    
    /**
     * Constructor.  
     * @param port 
     * @throws Exception 
     */
    public HttpAgent (int port) throws Exception {
        log.info("agent created...");
        initConfiguration();
        initServer(port);
        initHandlers();
    }

	public void start () throws Exception {
        log.debug("Initializing the Agent...");
        server.start();
    }
	
	private void initConfiguration () throws FileNotFoundException {
		this.config = new ClasspathPropertyFileConfiguration();
	}

	private void initServer (int port) {
    	server = new Server(port);
	}
	
	private void initHandlers () {
        Context root = new Context(server, CONTEXT_ROOT, Context.SESSIONS);
        server.setHandler(new JettyHttpCommandDispatchingHandler());
	}
}
